package cn.edu.zju.gis.disasterBE.sever.controller;

import cn.edu.zju.gis.disasterBE.sever.commons.annotation.PassToken;
import cn.edu.zju.gis.disasterBE.sever.commons.annotation.UserLoginToken;
import cn.edu.zju.gis.disasterBE.sever.commons.util.TokenUtil;
import cn.edu.zju.gis.disasterBE.sever.entity.Entity;
import cn.edu.zju.gis.disasterBE.sever.entity.News;
import cn.edu.zju.gis.disasterBE.sever.entity.User;
import cn.edu.zju.gis.disasterBE.sever.entity.assistants.UserAssistant;
import cn.edu.zju.gis.disasterBE.sever.entity.global.JsonAble;
import cn.edu.zju.gis.disasterBE.sever.entity.global.MyResponse;
import cn.edu.zju.gis.disasterBE.sever.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserController {
    private final UserService userService;

    @PassToken
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public MyResponse<User> registerUser(@RequestBody String json)
    {
        System.out.println("register in");
        return userService.registerUser(json);
    }

    @PassToken
    @ApiOperation("用户登录")
    @GetMapping("/login")
    public MyResponse<User> loginUser(@RequestParam String mailAddr, @RequestParam String pwd)
    {
        System.out.println("login in");
        return userService.loginUser(mailAddr,pwd);
    }

    @PassToken
    @ApiOperation("用户注册获取验证码")
    @GetMapping("/authCode")
    public MyResponse<String> sendVerification(@RequestParam String mailAddr)
    {
        System.out.println("authCode in");
        return userService.verifyEmail(mailAddr);
    }

    @UserLoginToken
    @ApiOperation("修改个人信息")
    @PostMapping("/updateData")
    public MyResponse<User> updateUser(@RequestBody String json)
    {
        System.out.println("update in");
        return userService.updateUser(json);
    }

    @UserLoginToken
    @ApiOperation("测试token")
    @GetMapping("/isLogin")
    public MyResponse<String> getMessage(){
        //System.out.println(TokenUtil.getTokenUserId());
        MyResponse<String> myResponse = new MyResponse<>();
        myResponse.setState(true);
        myResponse.setMsg("You have passed the verification");
        myResponse.setData(null);

        return myResponse;
    }

    @PassToken
    @ApiOperation("用户注册获取验证码")
    @GetMapping("/changePassword")
    public MyResponse<String> changePwd(@RequestParam Integer uid, @RequestParam String oldPwd, @RequestParam String newPwd)
    {
        User user = new User();
        user.setUid(uid);
        user.setPwd(oldPwd);
        return userService.changePwd(user,newPwd);
    }

    @PassToken
    @ApiOperation("用户忘记密码")
    @GetMapping("/forgetPassword")
    public MyResponse<String> forgetPwd(@RequestParam String mailAddr )
    {
        return userService.forgetPwd(mailAddr);
    }

    @PassToken
    @ApiOperation("认证")
    @GetMapping("/authentication")
    public MyResponse<String> authentication(@RequestParam String mailAddr )
    {
        return userService.authentication(mailAddr);
    }

    @ApiOperation("获得数据库中所有申请")
    @GetMapping("/getApplicationData")
    public MyResponse<List<UserAssistant>> getApplication()
    {
        return userService.getApplication();
    }

    @UserLoginToken
    @ApiOperation("修改个人信息")
    @GetMapping("/auditApplication")
    public MyResponse<String> auditApplication(@RequestParam String mailAddr)
    {
        return userService.auditApplication(mailAddr);
    }
}
